package com.atguigu.zhxy.service.impl;


import com.atguigu.zhxy.mapper.AdminMapper;
import com.atguigu.zhxy.pojo.Admin;
import com.atguigu.zhxy.pojo.LoginForm;
import com.atguigu.zhxy.service.TbAdminService;
import com.atguigu.zhxy.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Hoker
 * @description 针对表【tb_admin】的数据库操作Service实现
 * @createDate 2024-06-02 10:42:07
 */
@Service
@Transactional
public class TbAdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements TbAdminService {

    @Override
    public Admin login(LoginForm loginForm) {
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getName, loginForm.getUsername())
                          .eq(Admin::getPassword, MD5.encrypt(loginForm.getPassword()));
        Admin admin = baseMapper.selectOne(lambdaQueryWrapper);
        return admin;
    }

    @Override
    public IPage<Admin> getAdmins(Page<Admin> pageParam, String adminName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (adminName!=null) {
            queryWrapper.like("name", adminName);
        }

        queryWrapper.orderByDesc("id");
        queryWrapper.orderByAsc("name");
        return baseMapper.selectPage(pageParam, queryWrapper);
    }
}





